#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QBuffer>
#include <QXmlStreamWriter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->btn_qstring, &QPushButton::clicked, this, &MainWindow::onButtonClicked);
    connect(ui->btn_qbytearray, &QPushButton::clicked, this, &MainWindow::onButtonClicked);
    connect(ui->btn_qbuffer, &QPushButton::clicked, this, &MainWindow::onButtonClicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onButtonClicked()
{
    auto button = qobject_cast<QPushButton*>(sender());
    if (button == ui->btn_qstring) {
        QString output;
        QXmlStreamWriter writer(&output);
        writer.writeStartDocument();
        ui->textview->setPlainText(output.toUtf8());
    }
    if (button == ui->btn_qbytearray) {
        QByteArray output;
        QXmlStreamWriter writer(&output);
        writer.writeStartDocument();
        ui->textview->setPlainText(output);
    }
    if (button == ui->btn_qbuffer) {
        QBuffer output;
        output.open(QIODevice::WriteOnly);
        {
            QXmlStreamWriter writer(&output);
            writer.writeStartDocument();
        }
        output.close();
        ui->textview->setPlainText(output.data());
    }
}
